package com.xuzhijie.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Repair;
import com.xuzhijie.pms.entity.Visitor;
import com.xuzhijie.pms.service.IVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-17
 */
@RestController
@RequestMapping("/visitor")
public class VisitorController {

    @Autowired
    private IVisitorService service;

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer page,
                           @RequestParam Integer size,
                           @RequestParam Integer status) {

        return service.findPage(page, size,status);
    }

    @PostMapping("/addVisitor")
    public Result addVisitor(@RequestBody Map<String,Object> request){
        String description = request.get("description").toString();
        String address = request.get("address").toString();
        String owner = request.get("owner").toString();
        String nickname = request.get("nickname").toString();
        String type = request.get("type").toString();
        String status = request.get("status").toString();

        return service.addVisitor(description,address,owner,nickname,type,status);

    }
    @GetMapping("/wx-getVisitor")
    public Result getVisitor(@RequestParam String owner){

        return service.getVisitor(owner);
    }

    @GetMapping("wx-changeStatus")
    public Result changeStatus(@RequestParam String placesToVisit){
        Visitor v = service.getById(placesToVisit);
        v.setStatus(2);
        v.setApplicationTimeline(LocalDateTime.now());
        v.setRegisterSource(2);
        service.saveOrUpdate(v);
        return Result.success();
    }

    @PostMapping("/cgstatus")
    public Result changeStatus(@RequestBody Map<String,Object> request){
        System.out.println(request);
        String id = request.get("id").toString();
        Visitor byId = service.getById(id);
        byId.setStatus(2);
        byId.setApplicationTimeline(LocalDateTime.now());
        byId.setRegisterSource(1);
        service.saveOrUpdate(byId);
        return Result.success();
    }

    @GetMapping("/visitor-by-day")
    public List<Long> getVisitorDayByDay(){
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        List<Long> dayCount = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            LocalDate currentDate = now.minusDays(i).toLocalDate();
            //计算查询时间的起始和结束时间
            LocalDateTime startOfDate = currentDate.atStartOfDay();
            LocalDateTime endOfDate = startOfDate.plusHours(23).plusMinutes(59).plusSeconds(59);
            long count = service
                    .count(new LambdaQueryWrapper<Visitor>()
                            .between(Visitor::getApplicationTimeline, startOfDate, endOfDate));
            dayCount.add(count);
        }
        System.out.println(dayCount);
        return dayCount;
    }

}

